package com.sinodata.bsm.cicp.finder.tomcat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class MemoryFinder extends Finder {

    private static final Logger logger = Logger.getLogger(MemoryFinder.class);

    /*@Override
    public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
        List resources = new ArrayList();
        String ip = host;
        String port = attrVals[4].getAttrValue();
        String name = null;
        String[] attributeNames = new String[] { "Name", "Type" };
        Object attrValues[] = null;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connection = connector.getMBeanServerConnection();
            ObjectName memoryPool = new ObjectName("java.lang:type=MemoryPool,*");
            Set<ObjectName> set = connection.queryNames(memoryPool, null);
            for (ObjectName objectName : set) {
                attrValues = JmxUtil.getAttribute(connection, objectName, attributeNames);
                name = attrValues[0].toString();
                List oneResource = CreateOneResource(name, attributes);
                resources.add(oneResource);
            }
        } catch (Exception e) {
            logger.error("failed to find tomcat MemoryPool" + e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        return resources;
    }*/

    /* private List CreateOneResource(String name, ResAttributeValue[] attributes) {
         List aRes = new ArrayList();
         ResValue res = new ResValue();
         ResAttributeValValue valValue = null;
         res.setIp(host); // host IP address
         res.setInternalId(host + "_tomcatManager_" + name); // Internal ID for
         res.setName("Memory." + name);
         aRes.add(res);
         valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), name);
         aRes.add(valValue);
         return aRes;
     }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String port = (String) params.get("PORT");
        String name = null;
        String[] attributeNames = new String[] { "Name", "Type" };
        Object attrValues[] = null;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName memoryPool = new ObjectName("java.lang:type=MemoryPool,*");
            Set<ObjectName> set = connection.queryNames(memoryPool, null);
            for (ObjectName objectName : set) {
                attrValues = JmxUtil.getAttribute(connection, objectName, attributeNames);
                name = attrValues[0].toString();
                String[] info = new String[4];
                list.add(info);
                info[0] = ip + "_tomcatManager_" + name;
                info[1] = "Memory." + name;
                info[2] = "Memory " + name;
                info[3] = name; //名称
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find tomcat MemoryPool" + e);
            return null;
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
